package com.zy.field;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class student {

    @DefineField(name = "label")
    private String name;

    @DefineField(name = "num")
    private int age;

    @DefineField(name = "classInfo")
    private Classroom classroom;


    private List<Score> scores;

    public Classroom getClassroom() {
        return classroom;
    }

    public void setClassroom(Classroom classroom) {
        this.classroom = classroom;
    }

    public List<Score> getScores() {
        return scores;
    }

    public void setScores(List<Score> scores) {
        this.scores = scores;
    }

    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }




}

class Classroom {
    @DefineField(name = "roomName")
    private String name;

    @DefineField(name = "roomNumber")
    private String number;

    // getter/setter...


    public Classroom(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}

class Score {


    @DefineField(name = "subjectName")
    private String subject;


    private double score;

    // getter/setter...

    public Score(String subject, double score) {
        this.subject = subject;
        this.score = score;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

class Test {

    public static void main(String[] args) {
        // 场景一：使用自定义字段名 label 和 num
        student s = new student("张三", 18);
        s.setClassroom(new Classroom("A班", "101"));
        s.setScores(Arrays.asList(
                new Score("数学", 95.5),
                new Score("语文", 88.0)
        ));

        // 场景一：使用自定义字段名 label 和 num
        String jsonWithCustomNames = JSON.toJSONString(s, new SerializeConfig() {{
            put(student.class, new FlexibleObjectSerializer(true));
        }});
        System.out.println("自定义字段名: " + jsonWithCustomNames);

        // 场景二：使用原始字段名 name 和 age
        String jsonWithOriginalNames = JSON.toJSONString(s);
        System.out.println("原始字段名: " + jsonWithOriginalNames);
    }
}


 class TestCopy {
    public static void main(String[] args) {
        // 创建学生对象
        student s = new student("张三", 18);
        s.setClassroom(new Classroom("A班", "101"));
        s.setScores(Arrays.asList(
                new Score("数学", 95.5),
                new Score("语文", 88.0)
        ));

        // 使用自定义复制方法
        Map<String, Object> copiedObject = ObjectCopier.copyWithCustomNames(s);


        // 转换为JSON格式输出
        String jsonResult = JSON.toJSONString(copiedObject);
        System.out.println("JSON格式: " + jsonResult);

        System.out.println("复制前的对象: " + JSON.toJSONString(s));
    }
}


